import {
	fetchHome
} from '../../services/home/home';
import {
	fetchGoodsList
} from '../../services/good/fetchGoods';
import Toast from 'tdesign-miniprogram/toast/index';
const $api = require('../../utils/request').Api;
const app = getApp()
Page({
	data: {
    baseImgUrl: app.globalData.baseImgUrl,
		imgSrcs: [],
		tabList: [],
		goodsList: [],
		goodsListLoadStatus: 0,
		pageLoading: false,
		current: 1,
		autoplay: true,
		duration: '500',
		interval: 5000,
		navigation: {
			type: 'dots'
		},
		swiperImageProps: {
			mode: 'scaleToFill'
		},
		kong: 0
	},

	goodListPagination: {
		index: 0,
		num: 20,
	},

	privateData: {
		tabIndex: 0,
	},

	onShow() {
		// this.getTabBar().init();
	},

	onLoad(e) {
		console.log(e);
		this.init();
	},

	onReachBottom() {
		if (this.data.goodsListLoadStatus === 0) {
			this.loadGoodsList();
		}
	},

	onPullDownRefresh() {
		let index = 1
		this.init(index);
	},

	init(index) {
		this.loadHomePage(index);

	},

	loadHomePage(index) {
		wx.stopPullDownRefresh();

		this.setData({
			pageLoading: true,
		});
		fetchHome().then(({
			swiper,
			tabList
		}) => {
			this.setData({
				tabList,
				imgSrcs: swiper,
				pageLoading: false,
			});
			this.loadGoodsList(true, index);
		});
	},

	tabChangeHandle(e) {
		this.privateData.tabIndex = e.detail;
		this.loadGoodsList(true);
	},

	onReTry() {
		this.loadGoodsList();
	},

	loadGoodsList(fresh = false, index) {
		console.log(index);
		if (fresh) {
			wx.pageScrollTo({
				scrollTop: 0,
			});
		}

		this.setData({
			goodsListLoadStatus: 1
		});

		const pageSize = this.goodListPagination.num;
		let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
		if (fresh) {
			pageIndex = 1;
		}

		try {

			let nextList = index === 1 ? this.data.goodsList = [] : []
			// if (index === 1) {
			// 	this.data.goodsList = []
			// }
			$api.goodsList({
				page: pageIndex,
				size: 20,
				vin: ''
			}).then((res) => {
				// console.log(res.data);
				nextList = res.data.data
				this.setData({
					goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
					// goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
					goodsListLoadStatus: 0,
				});
			})
			// const nextList = await fetchGoodsList(pageIndex, pageSize);
			// // console.log(nextList);
			// this.setData({
			// 	goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
			// 	// goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
			// 	goodsListLoadStatus: 0,
			// });

			this.goodListPagination.index = pageIndex;
			this.goodListPagination.num = pageSize;
		} catch (err) {
			this.setData({
				goodsListLoadStatus: 3
			});
		}
	},

	goodListClickHandle(e) {
		const {
			index
		} = e.detail;
		const {
			productNumber
		} = this.data.goodsList[index];
		wx.navigateTo({
			url: `/pages/goods/details/index?spuId=${productNumber}`,
		});
	},

	goodListAddCartHandle() {
		Toast({
			context: this,
			selector: '#t-toast',
			message: '点击加入购物车',
		});
	},

	navToSearchPage() {
		wx.navigateTo({
			url: '/pages/goods/search/index'
		});
	},

	navToActivityDetail({
		detail
	}) {
		const {
			index: promotionID = 0
		} = detail || {};
		// // console.log(promotionID);
		// wx.navigateTo({
		// 	url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
		// });
		Toast({
			context: this,
			selector: '#t-toast',
			message: '该功能正在开发中!',
		});
	},
	scan: function (e) {
		console.log(e);
		let that = this;
		// //app.showError("功能开发中，敬请期待！");

		wx.scanCode({
			success: (res) => {
				// console.log(res.path.split('?')[1]);
				let add = res.path.split('?')[1]
				let arr = add[1]
				console.log(arr);
				//https://dl.lxcx365.com/index.php?m=Goods&a=g_list&m_id=1&mas_id=10&b_id=1&sort=1&type=breakfast

				// if (that.getUrlParam(res.result, "type") == "breakfast") {
				// 	var mId = that.getUrlParam(res.result, "m_id"),
				// 		masId = that.getUrlParam(res.result, "mas_id"),
				// 		bId = that.getUrlParam(res.result, "b_id"),
				// 		sort = that.getUrlParam(res.result, "sort");

				// if(that.data.isOrder){
				//     //有预约订单
				//     wx.navigateTo({
				//         url: '../breakfast/orderList?sort=' + sort + '&isBreakfirst=' + that.data.isBreakfirst,
				//     });
				// }else{
				//     //无预约订单
				//     if(!that.data.isBreakfirst || that.data.Breakfirst.button_sx == 2){
				//         // 未开启现场点餐
				//         app.showError("您还没有预约订单");
				//     }
				//     if(that.data.isBreakfirst && that.data.Breakfirst.button_sx == 1){
				// 已开启现场点餐
				wx.navigateTo({
					url: '/packageA/pages/Shophomepage/index?' + res.path.split('?')[1],
				});
				//     }
				// }

				// } else {
				// 	// res.result = "https://more.octopus.cn/now/order?a=58&p=35&td_channelid=31&uuid=36a15004525b550a16b410060b43e5f&va=1560163672";
				// 	//存入本地存储
				// 	wx.setStorage({
				// 		key: 'zyOder',
				// 		data: res.result
				// 	});

				// 	wx.navigateTo({
				// 		url: '../OutZy/out',
				// 	});
				// }
			}
		});
		// wx.navigateTo({
		//     // url: '../breakfast/index',
		//     // url: '../breakfast/appointment',
		//     // url: '../breakfast/orderList',
		//     url: '../breakfast/index?m_id=1&mas_id=10&b_id=1&sort=2',
		//     // url: '../groupWeek/index',
		//     // url: '../group/index',
		// });
	},
});